﻿using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Documents;
using UIWindows.Commands;
using UIWindows.GlobalStatics;
using UIWindows.Models;
using UIWindows.Tools.Helper;
using XingLucifer.IBase.Enums;
using Kinlo.Entitys;
using UIWindows.Enums;

namespace UIWindows.ViewModels
{
    public class DataItemConfigurationViewModel : XingLucifer.IBase.IViewStatus
    {
     
        public DisplayItemsStatic DisplayItems { get; set; }
        public List<string> DataTypeList { get; set; }
        private UserConfigStatic _userConfig;
        public DataItemConfigurationViewModel(DisplayItemsStatic displayItems, UserConfigStatic userConfig)
        {
            DisplayItems = displayItems;
            DataTypeList = new List<string>() { "无类型", "浮点型", "时间型", "结果" };
            _userConfig = userConfig;
            int count = 1;
            foreach (var item in ProductionData.Meta.AllFields)
            {
                if (!DisplayItems.WeighingDisplayItems.Any(x => x.Name == item.Name))
                {
                    count = DisplayItems.WeighingDisplayItems.Count + 1;
                    DisplayItems.WeighingDisplayItems.Add(new DisplayItemsModel()
                    {
                        Index = count,
                        Name = item.Name,
                        IsDisplay = true,
                        IsOutput = true,
                        Description = item.Description,
                        SqlName = item.Description,
                        DataType = item.Description.Contains("时间")
                        ? (item.Description.Contains("(s)") ? "无类型" : "时间型")
                        : (item.Description.Contains("g") || item.Description.Contains("kpa") || item.Description.Contains("ms") || item.Description.Contains("MΩ")) ? "浮点型" : "无类型",
                    });
                }
            }
            DisplayItems.SaveDisplayItem();
         
        }

        public Commands.DelegateCommand<DisplayItemsModel> SelectUpdate => new Commands.DelegateCommand<DisplayItemsModel>(sender =>
        {
            int count = 1;
            foreach (var item in DisplayItems.WeighingDisplayItems)
            {
                item.Index = count;
                count++;
            }
            DisplayItems.IsDataUpdate = true;
            DisplayItems.IsHomeUpdate = true;
        });
        public DelegateCommand UnLoadCMD => new DelegateCommand(() =>
        {
            try
            {
                DisplayItems.SaveDisplayItem();
                string msg = $"用户==[{_userConfig.LoginUser.Name}]：列表修改保存成功";
                msg.OperationLog(MessageLevelType.成功);
                _ = msg.MessageDialogs(MessageLevelType.成功, true, PopUpTypes.Global);
            }
            catch (Exception ex)
            {
                string msg = $"用户==[{_userConfig.LoginUser.Name}]：列表修改保存异常：{ex}";
                msg.OperationLog(MessageLevelType.成功);
                _ = msg.MessageDialogs(MessageLevelType.警告, true, PopUpTypes.Global);
            }
        });
        public void Load()
        {
        }

        public void UnLoad()
        {

        }
    }
}
